package com.qiqi.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    //创建二维数组
    int[][] data = new int[4][4];
    int[][] win = new int[][]{
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };
    String path = "image\\animal\\animal3\\";
    public GameJFrame(){
        //初始化界面
        initJFrame();
        //初始化菜单
        initMenuBar();
        initData();
        //初始化图片
        initImage();

        this.setVisible(true);
    }
    //二维数组 x y
    int x = 0;
    int y = 0;
    //初始化数据  打乱数组
    private void initData(){
        //随机数
        int[] tempArr = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        Random r = new Random();
        for (int i = 0; i < tempArr.length; i++) {
            //获取随机索引
            int index = r.nextInt(tempArr.length);
            int LinShi = tempArr[i];
            tempArr[i] = tempArr[index];
            tempArr[index] = LinShi;
        }

        //创建二维数组

        //添加数据
        for (int i = 0; i < tempArr.length; i++) {
            if(tempArr[i] == 0){
                x=i/4;
                y=i%4;
            }
                //  0/4  0   0%4  0
                //  1/4  0   1%4  1
                //  2/4  0   2%4  2
                //  ....
                data[i/4][i%4] = tempArr[i];
            System.out.println(tempArr[i]);

        }

    }
    int step = 0;
    private void initImage() {
        //清楚原本已经出现的图片
        this.getContentPane().removeAll();

        if(victory()){
            JLabel winJlabel = new JLabel(new ImageIcon("image\\win.png"));
            winJlabel.setBounds(203,283,197,73);
            this.getContentPane().add(winJlabel);
        }

        //统计步数
        JLabel stepCount = new JLabel("步数"+step);
        stepCount.setBounds(50,30,100,20);
        this.getContentPane().add(stepCount);

        for (int j = 0; j < data.length; j++) {
            for (int i = 0; i < data[j].length; i++) {
                //创建一个ImageIcon对象
                //创建JLabel的对象(容器管理)
                JLabel jLabel = new JLabel(new ImageIcon(path+data[j][i]+".jpg"));
                //指定图片位置
                jLabel.setBounds(105*i+83,j*105+134,105,105);
                //添加边框
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                this.getContentPane().add(jLabel);
            }
        }
        //添加图片
        JLabel bg = new JLabel(new ImageIcon("image\\background.png"));
        bg.setBounds(40,40,508,560);
        this.getContentPane().add(bg);
        //刷新界面
        this.getContentPane().repaint();


        //把容器管理添加到界面中

    }
    //创建选项下面的条目对象
    JMenuItem huanImage = new JMenu("更换图片");
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem accountItem = new JMenuItem("公众号");
    JMenuItem meinv = new JMenuItem("美女");
    JMenuItem dongwu = new JMenuItem("动物");
    JMenuItem yundong = new JMenuItem("运动");
    private void initMenuBar() {
        //初始化菜单
        //创建菜单对象
        JMenuBar jMenuBar = new JMenuBar();

        //创建菜单上面的两个选项的对象(功能  关于我们)
        JMenu functionJmenu = new JMenu("功能");
        JMenu aboutJmenu = new JMenu("关于我们");





        //将条目放进选项中
        functionJmenu.add(huanImage);
        functionJmenu.add(replayItem);
        functionJmenu.add(reLoginItem);
        functionJmenu.add(closeItem);

        aboutJmenu.add(accountItem);
        huanImage.add(meinv);
        huanImage.add(dongwu);
        huanImage.add(yundong);
        //给条目绑定书剑
        replayItem.addActionListener(this);
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        meinv.addActionListener(this);
        dongwu.addActionListener(this);
        yundong.addActionListener(this);

        //将菜单添加到菜单中
        jMenuBar.add(functionJmenu);
        jMenuBar.add(aboutJmenu);


        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        this.setSize(603,680);
        this.setTitle("拼图单机版 V1.0");
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //取消默认居中
        this.setLayout(null);
        //给整个界面添加键盘事件
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {


    }
    //按下不松
    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        if(code==65){
            //图片全部删除
            this.getContentPane().removeAll();
            //加载一图片
            JLabel all = new JLabel(new ImageIcon(path+"all.jpg"));
            all.setBounds(83,134,420,420);
            this.getContentPane().add(all);
            JLabel bg = new JLabel(new ImageIcon("image\\background.png"));
            bg.setBounds(40,40,508,560);
            this.getContentPane().add(bg);
            //刷新界面
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victory()){
            return;
        }
        //对上下左右判断
        //左37 上38  右39  下40
        int code = e.getKeyCode();
        if(code==37){
            if(y>=3) return;
            data[x][y] = data[x][y+1];
            data[x][y+1] = 0;
            y++;step++;

            //调用方法 按照最新的数字加载 图片
            initImage();
        }else if (code==38) {
            if(x>=3) return;
            data[x][y] = data[x+1][y];
            data[x+1][y] = 0;
            x++;step++;
            //调用方法 按照最新的数字加载 图片
            initImage();
        }else if (code==39) {
            if(y<=0) return;
            data[x][y] = data[x][y-1];
            data[x][y-1] = 0;
            y--;step++;
            //调用方法 按照最新的数字加载 图片
            initImage();
        }else if (code==40) {
            if(x<=0) return;
            data[x][y] = data[x-1][y];
            data[x-1][y] = 0;
            x--;step++;
            //调用方法 按照最新的数字加载 图片
            initImage();
        }else if (code == 65) {
            initImage();
        }else if(code == 87){
            System.out.println("测试");
            for (int i = 0; i < data.length; i++) {
                for (int j = 0; j < data[i].length; j++) {
                    data[i][j] =win[i][j];
                }
            }
            initImage();
        }

    }
    //判断data 数组数据是否跟win数组相同
    public boolean victory(){
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if(data[i][j] != win[i][j]) return false;
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();
        if(obj == replayItem){
            step = 0;
            initData();

            initImage();

        }else if (obj == reLoginItem) {
            this.setVisible(false);
            new LoginJFrame();
        }else if(obj == closeItem){
            System.exit(0);
        }else if(obj == accountItem){
            JDialog jDialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("image\\about.png"));
            jLabel.setBounds(0,0,258,258);
            jDialog.getContentPane().add(jLabel);
            jDialog.setSize(344,344);
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            jDialog.setModal(true);
            jDialog.setVisible(true);
        }else if(obj == meinv){
            Random r = new Random();
            path = "image\\girl\\girl"+r.nextInt(13)+"\\";
            step = 0;
            initData();
            initImage();

        }else if(obj == dongwu){
            Random r = new Random();
            path = "image\\animal\\animal"+r.nextInt(8)+"\\";
            step = 0;
            initData();
            initImage();

        }else if(obj == yundong){
            Random r = new Random();
            path = "image\\sport\\sport"+r.nextInt(10)+"\\";
            step = 0;
            initData();
            initImage();

        }
    }
}
